package com.community.controller;

import com.community.pojo.User;
import com.community.service.AdminService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {
    private final AdminService adminService;
    @Autowired
    public UserController(AdminService adminService) {
        this.adminService = adminService;
    }
    @GetMapping("/add-user-form")
    public String addUserFormController(){
        return "addUserForm";
    }
    @PostMapping("/add-user")
    public String addUserController(@ModelAttribute("user") User user, Model model, HttpSession session){
        if(user.getUsername().equals("")||user.getPassword().equals("")){
            model.addAttribute("message", "用户名或密码不允许为空! " );
            if(session.getAttribute("user") != null){
                return "addUserForm";
            }else{
                return "signUpForm";
            }

        }
        User findUser= adminService.findUserByUsername(user.getUsername());
        if(findUser!=null){
            model.addAttribute("message", "用户名为: "+findUser.getUsername()+"已经存在，不允许重复注册！" );
            if(session.getAttribute("user") != null){
                return "addUserForm";
            }else{
                return "signUpForm";
            }
        }
        int n= adminService.addUser(user);
        if(n==1){
            int id = user.getUserId();
            model.addAttribute("message", "注册成功，新注册的用户id为: " + id);
        }else{
            model.addAttribute("message", "注册失败!");
        }
        if(session.getAttribute("user") == null){
            return "loginForm";
        }else{
            return "addUserResult";
        }
    }

    @GetMapping("/sign-up-form")
    public String signUpFormController() {
        return "signUpForm";
    }


    @GetMapping("/delete-user-form")
    public String deleteUserFormController(){
        return "deleteUserForm";
    }
    @RequestMapping("/delete-user")
    public String deleteUserController(@RequestParam("userId") String userId, Model model){
        if(userId.equals("")){
            model.addAttribute("message", "id不允许为空! " );
            return "deleteUserForm";
        }
        int id=Integer.parseInt(userId);
        User findUser= adminService.findUser(id);
        if(findUser==null){
            model.addAttribute("message", "用户id为: "+id+"的用户不存在，无法删除！" );
            return "deleteUserForm";
        }
        int n= adminService.deleteUser(id);
        if(n==1){
            model.addAttribute("message","已成功删除id为: " + id+"的用户");
        }else{
            model.addAttribute("message","id为: " + id+"的用户不存在，无法删除!");
        }
        return "deleteUserResult";
    }

    @GetMapping("/update-user-information-form")
    public String updateUserInformationFormController(@RequestParam Integer userId, Model model){
        User user= adminService.findUser(userId);
        model.addAttribute("user", user);
        return "updateUserInformationForm";
    }
    @PostMapping("/update-user-information")
    public String updateUserInformationController(@ModelAttribute User user, Model model){
        if(user.getUsername().equals("")||user.getPassword().equals("")){
            model.addAttribute("message","用户名或密码不能为空!");
            return "updateUserInformationForm";
        }
        User findUser= adminService.findUserByUsername(user.getUsername());
        if(findUser!=null&&user.getUserId()!=findUser.getUserId()){
            model.addAttribute("message","该用户名已被占用，请你再换一个!");
            return "updateUserInformationForm";
        }
        int n= adminService.updateUser(user);
        if(n==1){
            model.addAttribute("message","修改成功!");
            User updatedUser = adminService.findUser(user.getUserId());
            model.addAttribute("findUser", updatedUser);
        }else{
            model.addAttribute("message","修改失败!");
        }
        return "findUserResult";
    }

    @GetMapping("/find-user-form")
    public String selectUserFormController(){
        return "findUserForm";
    }
    @PostMapping("/find-user")
    public String findUserController(@RequestParam("userId") String userId, Model model){
        if(userId.equals("")){
            model.addAttribute("message","查询id不能为空!");
            return "findUserForm";
        }
        Integer id = Integer.parseInt(userId);
        User findUser= adminService.findUser(id);
        if(findUser==null){
            model.addAttribute("message","该id的用户不存在!");
            return "findUserForm";
        }else{
            model.addAttribute("findUser",findUser);
            return "findUserResult";
        }
    }

    @GetMapping("/find-all-user")
    public String findAllUserFormController(Model model){
        List<User> userList= adminService.findAllUser();
        if(userList.size()==0){
            model.addAttribute("message","查无结果!");
        }else{
            model.addAttribute("userList",userList);
        }
        return "findAllUserResult";
    }



}
